#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

int global=0;

pthread_mutex_t mtx;

void* start_routine(void * arg){
	int loops=*(int *)arg;
	for(int i=0;i<loops;i++){
		pthread_mutex_lock(&mtx);
		global++;
		pthread_mutex_unlock(&mtx);
	}
	pthread_exit(NULL);
}

int main(int argc,char *argv[]){
	if(argc!=2){
		return 1;
	}
	int loopCount=atoi(argv[1]);
	pthread_t thread[2]={0};
	int ret=0;
	pthread_mutex_init(&mtx,NULL);
	for(int i=0 ;i<2;i++){
		ret=pthread_create(thread+i,NULL,start_routine,&loopCount);
		if(ret!=0){
			perror("pthread_create");
			exit(1);
		}
	}
	pthread_join(thread[0],NULL);
	pthread_join(thread[1],NULL);
	pthread_mutex_destroy(&mtx);
	printf("global=%d\n",global);
    return 0;
}
